﻿Imports System.Data
Imports System.Data.OleDb

Public Class frmEditRapport
    Dim SQLStr As String
    Private ConnString As String
    Dim _rapport As Projekt = Projekt.GetObject()

    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=|DataDirectory|\db.accdb")
    Dim reader As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim mitarbeiterid As String

    Private Sub frmEditRapport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (_rapport.rapportEdit) Then
            SQLStr = "SELECT * FROM [Rapporte] WHERE [PK_Rapporte] = " + _rapport.rapportrowId
            cmd.CommandText = SQLStr
            cmd.Connection = conn
            cmd.Connection.Open()
            reader = cmd.ExecuteReader()

            Do While reader.Read()
                txbBemerkung.Text = reader("Bemerkung").ToString()
                txbDatum.Text = reader("Datum").ToString()
                txbZeit.Text = reader("Zeit").ToString()
                mitarbeiterid = reader("FK_Mitarbeiter").ToString()
            Loop
            cmd.Connection.Close()


            readAufgabe()
            readMitarbeiter(mitarbeiterid)
        Else
            readAufgabe()
            txbMitarbeiter.Text = _rapport.mitarbeiter
            txbMitarbeiter.Enabled = False
        End If


    End Sub

    Private Sub readAufgabe()
        SQLStr = "SELECT [Titel] FROM [Aufgaben] WHERE [PK_Aufgaben] = " + _rapport.aufgabenrowId
        cmd.CommandText = SQLStr
        cmd.Connection = conn
        cmd.Connection.Open()
        reader = cmd.ExecuteReader()

        Do While reader.Read()
            txbAufgabe.Text = reader("Titel").ToString()
        Loop
        cmd.Connection.Close()
        txbAufgabe.Enabled = False

    End Sub

    Private Sub readMitarbeiter(ByVal id As String)
        SQLStr = "SELECT [Benutzername] FROM [Mitarbeiter] WHERE [PK_Mitarbeiter] = " + mitarbeiterid.ToString()
        cmd.CommandText = SQLStr
        cmd.Connection = conn
        cmd.Connection.Open()
        reader = cmd.ExecuteReader()

        Do While reader.Read()
            txbMitarbeiter.Text = reader("Benutzername").ToString()
        Loop
        cmd.Connection.Close()
        txbMitarbeiter.Enabled = False

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (_rapport.rapportEdit = True) Then
            SQLStr = "UPDATE [Rapporte] SET [FK_Aufgabe] = " + _rapport.aufgabenrowId + ", [Datum] = '" + txbDatum.Text + "', [Bemerkung] = " + txbBemerkung.Text + ", [FK_Mitarbeiter] = " + mitarbeiterid + ", [Zeit] = " + txbZeit.Text + " Where [PK_Aufgaben] = " + _rapport.rapportrowId + ";"
        Else
            SQLStr = "INSERT Into [Rapporte] ([FK_Aufgabe], [Datum], [Bemerkung], [FK_Mitarbeiter], [Zeit]) VALUES(" + _rapport.aufgabenrowId + ", " + txbDatum.Text + ", " + txbBemerkung.Text + ", " + _rapport.mitarbeiter + ", " + txbZeit.Text + ") WHERE [PK_Rapporte] = 1"
        End If

        executeQuery(SQLStr)
    End Sub

    Private Sub executeQuery(ByVal sqlquery As String)
        Try
            cmd.CommandText = SQLStr
            cmd.Connection = conn
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

    Private Sub frmEditRapport_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        _rapport.rapportEdit = False
        frmShowRapporte.Show()
    End Sub
End Class